<template>
  <div>
    <div class="top">
      <el-button @click="dialogFormVisible = true">+ 添加设置</el-button>
      <el-dialog
        title="添加活动"
        :visible.sync="dialogFormVisible"
        width="450px"
      >
        <el-form :model="form">
          <el-form-item label="活动资金" :label-width="formLabelWidth">
            <el-input v-model="form.monney" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="参与人数" :label-width="formLabelWidth">
            <el-input v-model="form.joinNum" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="入围人数" :label-width="formLabelWidth">
            <el-input v-model="form.intoNum" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="入围限制说明" :label-width="formLabelWidth">
            <el-input v-model="form.intoInfo" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="有效期" :label-width="formLabelWidth">
            <el-date-picker
              format="yyyy-MM-dd"
              v-model="form.userfulDate"
              type="date"
              placeholder="选择日期"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="添加日期" :label-width="formLabelWidth">
            <el-date-picker
              format="yyyy-MM-dd"
              v-model="form.addDate"
              type="date"
              placeholder="选择日期"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="结束时间" :label-width="formLabelWidth">
            <el-date-picker
              format="yyyy-MM-dd"
              v-model="form.endDate"
              type="date"
              placeholder="选择日期"
            >
            </el-date-picker>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="addInfo">确 定</el-button>
        </div>
      </el-dialog>
    </div>
    <div class="table">
      <el-table :data="infoList.slice((currentPage-1)*pagesize,currentPage*pagesize)" style="width: 100%">
        <el-table-column align="center" prop="monney" label="可分配资金">
        </el-table-column>
        <el-table-column align="center" prop="joinNum" label="参与人数">
        </el-table-column>
        <el-table-column
          align="center"
          prop="intoNum"
          label="入围人数"
        ></el-table-column>
        <el-table-column align="center" prop="userfulDate" label="有效期">
        </el-table-column>
        <el-table-column align="center" prop="addDate" label="添加时间">
        </el-table-column>
        <el-table-column
          align="center"
          prop="endDate"
          label="结束时间"
        ></el-table-column>
        <el-table-column align="center" label="操作">
          <template slot-scope="scope">
            <el-button @click="setInfo(scope.row)">编辑</el-button>
            <el-button @click="delteInfo(scope.row._id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5, 10, 20, 40]"
        :page-size="pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="infoList.length"
      >
      </el-pagination>
      <el-dialog
        title="修改活动"
        :visible.sync="dialogFormVisible1"
        width="450px"
      >
        <el-form :model="form1">
          <el-form-item label="活动资金" :label-width="formLabelWidth">
            <el-input v-model="form1.monney" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="参与人数" :label-width="formLabelWidth">
            <el-input v-model="form1.joinNum" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="入围人数" :label-width="formLabelWidth">
            <el-input v-model="form1.intoNum" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="入围限制说明" :label-width="formLabelWidth">
            <el-input v-model="form1.intoInfo" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="有效期" :label-width="formLabelWidth">
            <el-date-picker
              format="yyyy-MM-dd"
              v-model="form1.userfulDate"
              type="date"
              placeholder="选择日期"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="结束时间" :label-width="formLabelWidth">
            <el-date-picker
              format="yyyy-MM-dd"
              v-model="form1.endDate"
              type="date"
              placeholder="选择日期"
            >
            </el-date-picker>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible1 = false">取 消</el-button>
          <el-button type="primary" @click="updateInfo">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import axios from "axios";
export default {
  data() {
    return {
      dialogFormVisible: false,
      dialogFormVisible1: false,
      currentPage: 1, //初始页
      pagesize: 5, //    每页的数据
      infoList: [
        {
          addDate: "",
          endDate: "",
          intoInfo: "",
          intoNum: 0,
          joinNum: 0,
          monney: 0,
          userfulDate: "",
          _id: "",
        },
      ],
      form: {
        addDate: "",
        endDate: "",
        intoInfo: "",
        intoNum: 0,
        joinNum: 0,
        monney: 0,
        userfulDate: "",
      },
      form1: {
        addDate: "",
        endDate: "",
        intoInfo: "",
        intoNum: 0,
        joinNum: 0,
        monney: 0,
        userfulDate: "",
        _id: "",
      },
      formLabelWidth: "120px",
    };
  },
  created() {
    this.getInfo();
  },
  methods: {
    getInfo() {
      axios.get("http://47.108.201.43:3000/api/getSetup").then((res) => {
        console.log(res.data);
        this.infoList = res.data.data;
      });
    },
    addInfo() {
      if (
        this.form.endDate == "" ||
        this.form.intoInfo == "" ||
        this.form.intoNum == null ||
        this.form.joinNum == null ||
        this.form.monney == null ||
        this.form.userfulDate == ""
      ) {
        this.$message({
          message: "参数不能为空！",
          type: "error",
        });
        return;
      } else {
        axios
          .post("http://47.108.201.43:3000/api/addSetup", {
            addDate: this.form.addDate,
            endDate: this.form.endDate,
            intoInfo: this.form.intoInfo,
            intoNum: this.form.intoNum,
            joinNum: this.form.joinNum,
            monney: this.form.monney,
            userfulDate: this.form.userfulDate,
          })
          .then((res) => {
            console.log(res.data);
            if (res.data.code == 200) {
              this.$message({
                message: "添加成功！",
                type: "success",
              });
              this.dialogFormVisible = false;
              this.getInfo();
            } else {
              this.$message({
                message: "添加失败！",
                type: "error",
              });
            }
          });
      }
    },
    delteInfo(id) {
      axios
        .post("http://47.108.201.43:3000/api/deleteSetup", {
          _id: id,
        })
        .then((res) => {
          console.log(res.data);
          if (res.data.code == 200) {
            this.$message({
              message: "删除成功！",
              type: "success",
            });
            this.getInfo();
          }
        });
    },
    updateInfo() {
      if (
        this.form1.endDate == "" ||
        this.form1.intoInfo == "" ||
        this.form1.intoNum == null ||
        this.form1.joinNum == null ||
        this.form1.monney == null ||
        this.form1.userfulDate == ""
      ) {
        this.$message({
          message: "参数不能为空！",
          type: "error",
        });
        return;
      } else {
        axios
          .post("http://47.108.201.43:3000/api/changeSetup", {
            addDate: this.form1.addDate,
            _id: this.form1._id,
            endDate: this.form1.endDate,
            intoInfo: this.form1.intoInfo,
            intoNum: this.form1.intoNum,
            joinNum: this.form1.joinNum,
            monney: this.form1.monney,
            userfulDate: this.form1.userfulDate,
          })
          .then((res) => {
            console.log(res.data);
            if (res.data.code == 200) {
              this.$message({
                message: "修改成功！",
                type: "success",
              });
              this.dialogFormVisible1 = false;
              this.getInfo();
            } else {
              this.$message({
                message: "添加失败！",
                type: "error",
              });
            }
          });
      }
    },
    setInfo(info) {
      this.form1 = info;
      this.dialogFormVisible1 = true;
    },
     handleSizeChange: function (size) {
      this.pagesize = size;
      console.log(this.pagesize); //每页下拉显示数据
    },
    handleCurrentChange: function (currentPage) {
      this.currentPage = currentPage;
      console.log(this.currentPage); //点击第几页
    },
  },
};
</script>
<style scoped>
.top {
  margin-top: 15px;
  margin-left: 15px;
}
.table {
  margin: 10px;
}
</style>
